package com.cwz.auth.service;

import com.cwz.api.entity.SysLogininfor;
import com.cwz.api.remote.RemoteLogService;
import com.cwz.core.constant.Constants;
import com.cwz.core.constant.SecurityConstants;
import com.cwz.core.utils.date.DateUtils;
import com.cwz.core.utils.ip.IpUtils;
import com.cwz.core.utils.servle.ServletUtils;
import com.cwz.core.utils.string.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @program: w-demo
 * @description: 记录日志方法
 * @author: Wen
 **/
@Service
public class SysRecordLogService {
	@Autowired
	private RemoteLogService remoteLogService;

	/**
	 * @Description: 记录登录信息
	 * @Author: Wen
	 * @param username: 	用户名
	 * @param status: 		状态
	 * @param message:  	消息内容
	 * @return: void
	 **/
	public void recordLogininfor(String username, String status, String message) {
		SysLogininfor logininfor = new SysLogininfor();
		logininfor.setUserName(username);
		logininfor.setIpaddr(IpUtils.getIpAddr(ServletUtils.getRequest()));
		logininfor.setMsg(message);
		logininfor.setAccessTime(DateUtils.getNowDate());
		// 日志记录
		if (StringUtils.equalsAny(status, Constants.LOGIN_SUCCESS, Constants.LOGOUT, Constants.REGISTER)) {
			logininfor.setStatus(Constants.LOGIN_SUCCESS_STATUS);
		}
		else if (Constants.LOGIN_FAIL.equals(status)) {
			logininfor.setStatus(Constants.LOGIN_FAIL_STATUS);
		}
		this.remoteLogService.saveLogininfor(logininfor, SecurityConstants.INNER);
	}

}
